<?php
namespace App\Models\Factory;

use App\Models\Orm\AdminUsers;
use App\Models\Orm\DataDeliveryCount;
use App\Models\AbsModelFactory;
use App\Models\Orm\DataDeliveryDownloadLog;
use App\Models\Orm\Deliver;
use App\Models\Orm\Delivery;
use App\Models\Orm\DeliveryLog;
use App\Models\Orm\CooperativeCompany;
use App\Models\Orm\DeliveryAdmin;

/**
 * @file DeliveryFactory.php
 * @brief 渠道业务工厂方法
 * @author 
 * @version 1.6
 * @date 2017-09-22
 */
class DeliveryFactory extends  AbsModelFactory
{

	/**
	 * @brief getAllChannel 获取所有渠道
	 *
	 * @return array
	 */
    public static function getAllChannel()
    {
        $channels = Delivery::where('status',1)->get()->toArray();
        return $channels ? : [];
    }


    /**
     * 获取某渠道前天总的登录次数 type为2或者type为3的
     * @param $channel_id
     * @param $create_date
     * @return int
     */
    public static  function getTotalLogin($channel_id, $create_date)
    {
        $total_login = DeliveryLog::where('channel_id',$channel_id)->where('create_date','<',$create_date . ' 24:00:00')->where('create_date','>=',$create_date . ' 00:00:00')->whereIn('type',[2,3])->count();
        return $total_login ?: 0;
    }


    /**
     * 获取某渠道前天总的登录人数 type为2或者type为3的以user_id分组之后的组个数
     * @param $channel_id
     * @param $create_date
     * @return int
     */
    public static function getTotalLoginUser($channel_id, $create_date)
    {
        $total_login_user = DeliveryLog::where('channel_id',$channel_id)->where('create_date','<',$create_date . ' 24:00:00')->where('create_date','>=',$create_date . ' 00:00:00')->whereIn('type',[2,3])->groupBy('user_id')->get()->toArray();
        return $total_login_user ? count($total_login_user): 0;
    }


    /**
     *获取渠道当日uv
     * @param $channel_id
     * @param $create_date
     * @return int
     */
    public static function getTotalUv($channel_id, $create_date)
    {
        $total_uv = DeliveryLog::where('channel_id',$channel_id)->where('create_date','<',$create_date . ' 24:00:00')->where('create_date','>=',$create_date . ' 00:00:00')->groupBy('create_ip')->get()->toArray();
        return $total_uv ? count($total_uv): 0;
    }


	/**
	 * @brief getTodayData 单个渠道的今日数据
	 *
	 * @param $channel_id
	 * @param $type
	 *
	 * @return int
	 */
    public static function getTodayData($channel_id,$type)
    {
        $today = date('Y-m-d',time());
        $count = DeliveryLog::where('channel_id',$channel_id)->where('create_date','>',$today . ' 00:00:00')->where('create_date','<=', $today. '24:00:00')->where('type',$type)->count();
        return $count ? : 0;
    }


	/**
	 * @brief getHistoryData 单个渠道历史数据
	 *
	 * @param $channel_id
	 *
	 * @return array
	 */
    public static function getHistoryData($channel_id)
    {
        $data = DataDeliveryCount::where('channel_id',$channel_id)->orderBy('create_date','desc')->get();
        return $data ? : [];
    }


	/**
	 * @brief getAccountUserId 获取安稳号用户id
	 *
	 * @param $channel_id
	 *
	 * @return int
	 */
    public static function getAccountUserId($channel_id)
    {
        $channel = Delivery::find($channel_id);

        return $channel ? $channel->admin_user_id : 0;
    }

	

	/**
	 * @brief getAccounts 获取所有的企业
	 *
	 * @return array
	 */
	public static function getAccounts()
	{
		$accounts = CooperativeCompany::where('is_auth',1)->get()->toArray();
		return $accounts ? : [];
	
	}


    /**
     * @brief getAccounts 获取所有的企业
     *
     * @return array
     */
    public static function getAccountsIdNameArray()
    {
        $accounts = CooperativeCompany::where('is_auth',1)->get()->toArray();
        return $accounts ? array_column($accounts,'company_name','id'): [];

    }


	/**
	 * @brief getAccountInfoById 通过id获取企业信息
	 *
	 * @param $company_id 
	 *
	 * @return  array
	 */
	public static function getAccountInfoById($company_id)
	{
		$company = CooperativeCompany::where('id',$company_id)->get()->toArray();
		return $company ? : [];
	}


	/**
	 * @brief getCompanyIdByChannelId 通过渠道id获取渠道关联的企业id
	 *
	 * @param $channel_id
	 *
	 * @return int
	 */
	public static function getCompanyIdByChannelId($channel_id)
    {
        $delivery = Delivery::find($channel_id);
        return $delivery ? $delivery->company_id : 0;
    }


	/**
	 * @brief getChannelById 通过渠道id获取渠道信息
	 *
	 * @param $channel_id
	 *
	 * @return object
	 */
	public static function getChannelById($channel_id)
	{
		$channel = Delivery::find($channel_id);
		return $channel;
	}


	/**
	 * @brief getChannelRegisterTotal 单个渠道总注册	 
	 *
	 * @param $channel_id
	 *
	 * @return int
	 */
	public static function getChannelRegisterTotal($channel_id)
	{
		$register = DataDeliveryCount::where('channel_id',$channel_id)->sum('register');
		return $register ? : 0;
	}

	
	/**
	 * @brief getChannelAdminEmail 获取安稳号用户id获取登录邮箱
	 *
	 * @param $admin_user_id
	 *
	 * @return string
	 */
	protected function getChannelAdminEmail($admin_user_id)
	{
		$channel_admin = DeliveryAdmin::where('id',$admin_user_id)->get()->toArray();
		return $channel_admin ? $channel_admin->email	: '';
	
	}
	
	
	
	/**
	 * @brief yesterdayData 昨日渠道数据
	 *
	 * @return array
	 */
	public static function yesterdayData()
	{
		$date = date('Y-m-d',time()-86400);
		$yesterday = DataDeliveryCount::where('create_date','=',$date)->get()->toArray();
		return $yesterday ? : [];
	}


    /**
     * @param $key
     * @param $channel_id
     * @return string
     */
    public static function yesterday($key,$channel_id)
    {
        $date = date('Y-m-d',time()-86400);
        $yesterday = DataDeliveryCount::where('create_date','=',$date)->where('channel_id',$channel_id)->first();
        return $yesterday ? $yesterday->$key : '-';
    }

    /**
     * 昨天某一项数据转百分比显示
     *
     * @param $key
     * @param $channel_id
     * @return string
     */
    public static function yesterdayToPercent($key, $channel_id)
    {
        $date = date('Y-m-d', time() - 86400);
        $yesterday = DataDeliveryCount::where('create_date', '=', $date)->where('channel_id', $channel_id)->first();

        if ($yesterday) {
            return ($yesterday->$key * 100) . '%';
        }

        return '-';
    }

    /**
     * 获取安稳号后台用户id
     * @param $channel_id
     * @param $company_id
     * @return string
     */
	protected  function getAnwenhaoAdminUserId($channel_id,$company_id)
    {
        $channel = Delivery::select('admin_user_id')->where('channel_id',$channel_id)->where('company_id',$company_id)->get()->toArray();
        $admin_user_id = isset($channel['admin_user_id']) ? $channel['admin_user_id'] : '';
        return $admin_user_id;
    }


    /**
     * 通过登录邮箱获取安稳号后台用户id
     * @param $email
     * @return int
     */
    public static function getAdminUserIdByEmail($email)
    {
        $admin_user = AdminUsers::where('email',$email)->first();
        return $admin_user ? $admin_user->id : 0;
    }


    //通过渠道id计算出当天下载量
    public static  function getTotalDownload($channel_id, $type)
    {
        $today = date('Y-m-d',time());
        $count = DeliveryLog::where('channel_id',$channel_id)->where('create_date','>',$today . ' 00:00:00')->where('create_date','<=', $today. '24:00:00')->where('type',$type)->count();
        return $count ? : 0;
    }

    /**
     * 根据渠道ID获取所属公司名称
     *
     * @param $channel_id
     * @return string
     */
    public static function getCompanyNameByChannelId($channel_id)
    {
        $company = self::getCompanyByChannelId($channel_id);
        return $company ? $company->company_name : ' ';
    }

    /**
     * 根据渠道ID获取所属公司信息
     * @param $channel_id
     * @return null
     */
    public static function getCompanyByChannelId($channel_id)
    {

        $channel = Deliver::find($channel_id);

        return $channel ? $channel->getCompany() : null;
    }

}
